<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		formShowLoading,
		getEditorTitle,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				userCacheKey:"hr-zy-tpersonalCareerDevelop-user-cache"
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			formSubmit(){
				let _that = this
				_that.om.formSave("tpersonalCareerDevelop/addTpersonalCareerDevelop",_that.formdata,_that)
			},
			verifyTitle3(){
				let _that = this
				if(!(_that.subEditorObj["title2"].workList.length>0)){
					let title = _that.$refs.autoform.getEditorTitle("title2","objStr1")
					let subtitle = getEditorTitle("title2","posId",_that.subConfigData["title2"])
					_that.om.toast("请先选择"+title+"中的"+subtitle)
					return false
				}else{
					return true
				}
			},
			getWorkParam(){
				let _that = this
				return{
					posId:[_that.$refs.autoform.formdata.posId]
				}
			},
			getWorkParam1(){
				let _that = this
				let arr = new Array()
				arr = _that.subEditorObj["title2"].workList.map(e=> e.posId)
				return {
					posIdList:arr
				}
			},
			onSelectPos(item){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				formdata.posFeature = item.featureName
				formdata.salaryWay = item.salarycatName
				formdata.instShortName = item.instShortName
				formdata.posGrade = item.careercatName
				formdata.workDate = item.workdate
			},
			getPosList(userId){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/getPosComboboxByUser",
					{
						userId:userId
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title1","posId","data",configSelectData(res,"id","posname"))
					_that.$refs.autoform.initSelectIndex("title1","posId")
				})
			},
			getUserList(zposId){
				let _that = this
				_that.om.request(
					"tpersonalCareerDevelop/getUserList",
					{
						zposId: zposId
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title1","userId",{
						link:"../../public/pageSearch/pageSearch",
						"linkParam":{
							other:JSON.stringify(),
							defaultData:JSON.stringify(res),
							namefield:"username",
							cachename:_that.userCacheKey,
							initload:true,
							placeholder:"请输入姓名"
						}
					})
					if(!_that.edit){
						_that.$refs.autoform.formdata.userId = res[0].id
						_that.$refs.autoform.formdata.userName = res[0].username
						_that.$refs.autoform.formdata.posId =res[0].salaryPosId
						_that.$refs.autoform.formdata.posName = res[0].salaryPosName
						_that.$refs.autoform.formdata.sexName = res[0].sexName
						_that.$refs.autoform.formdata.entryDate = res[0].entrydate
						_that.$refs.autoform.formdata.instShortName = res[0].salaryPosInstName
						_that.$refs.autoform.formdata.posGrade = res[0].salaryLevelName
						_that.$refs.autoform.formdata.posFeature = res[0].featureName
						_that.$refs.autoform.formdata.salaryWay = res[0].salarycatName
						_that.$refs.autoform.formdata.workDate = res[0].entrydate
						_that.getPosList(res[0].id)
					}
				})
			},
			initLoad(options){
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code)
				]
				let fileId = options.fileId
				if(fileId){
					ajaxArr.push({
						url:"tpersonalCareerDevelop/getInfo",
						param:{
							id:fileId
						},
						method:"post"
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname",true),mainData)
					if(!fileId){
						_that.getUserList(res[0][0].id)
					}
					if(_that.subConfigData["title2"]){
						let configData = _that.subConfigData["title2"]
						let attr = {}
						attr.getWorkParam = "getWorkParam"
						if(hasEditor("title2","posId",configData)){
							let title = getEditorTitle("title2","posId",configData)
							attr.titleTemplate = title+':<%= posName||"未填写" %>'
						}
						changeEditor('title2', 'objStr1', attr, mainData);
					}
					if(_that.subConfigData["title3"]){
						let configData = _that.subConfigData["title3"]
						let attr = {}
						attr.getWorkParam = "getWorkParam1"
						if(hasEditor("title3","posId",configData)){
							let title = getEditorTitle("title3","posId",configData)
							attr.titleTemplate = title+':<%= posName||"未填写" %>'
						}
						changeEditor('title3', 'objStr2', attr, mainData);
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[1].value.t){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value.t[index])
								_that.$set(_that.formdata,index,res[1].value.t[index])
							}
						}
						if(_that.subEditorObj["title2"]){
							let tds1 = _that._.cloneDeep(res[1].value.tds1)
							_that.subEditorObj["title2"].workList = tds1
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"objStr1",JSON.stringify(tds1))
							_that.$set(_that.formdata,"objStr1",JSON.stringify(tds1))
						}
						if(_that.subEditorObj["title3"]){
							let tds2 = _that._.cloneDeep(res[1].value.tds2)
							_that.subEditorObj["title3"].workList = tds2
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"objStr2",JSON.stringify(tds2))
							_that.$set(_that.formdata,"objStr2",JSON.stringify(tds2))
						}
						_that.$nextTick(function(){
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
							_that.getPosList(_that.$refs.autoform.formdata.userId)
							_that.getUserList(_that.$refs.autoform.formdata.zposId)
						})
					}
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for(let fieldId in _that.subEditorObj){
					let editor = _that.subEditorObj[fieldId]
					uni.getStorage({
						key:getWorkCacheKey(fieldId),
						success:(res)=>{
							editor.workList = res.data
							uni.removeStorage({
								key:getWorkCacheKey(fieldId)
							})
						}
					})
				}
				uni.getStorage({
					key:_that.userCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userId = d.id
							formdata.userName = d.username
							formdata.posId =d.salaryPosId
							formdata.posName = d.salaryPosName
							formdata.sexName = d.sexName
							formdata.entryDate = d.entrydate
							formdata.instShortName = d.salaryPosInstName
							formdata.posGrade = d.salaryLevelName
							formdata.posFeature = d.featureName
							formdata.salaryWay = d.salarycatName
							formdata.workDate = d.entrydate
							_that.getPosList(d.id)
							_that.subEditorObj["title2"].workList = []
							_that.subEditorObj["title3"].workList = []
							uni.removeStorage({
								key:_that.userCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style></style>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
